<?php

namespace App\AdminApi\Module\Admin\Form;

use App\AdminApi\Module\Admin\Service\DepartmentService;
use App\Common\Model\Admin\DepartmentModel;
use App\Sys;
use Zms\AdminElementPlus\FormCreate\Form;
use Zms\AdminElementPlus\FormCreate\FormItem;
use Zms\Constants\Database;
use Zms\Exception\AppException;

class DepartmentForm
{


    /**
     * 创建部门表单
     */
    public function createdForm($pid = 0): Form
    {
        $rule[] = FormItem::Hidden('pid', $pid ?: 0);
//        $rule[] = FormItem::SearchPanel('manage_id', '部门主管', '/admin-api/filter-panel/depauth-user')
//            ->columns([
//                ['prop' => 'id', 'label' => 'ID'],
//                ['prop' => 'nickname', 'label' => '名称'],
//            ])
//            ->labelKey('nickname')
//            ->required('请选择部门主管');
        $rule[] = FormItem::Input('name', '部门名称')->placeholder('后台部门名称')->clearable(true)->maxlength(10)->required('请输入部门名称');
        $rule[] = FormItem::Number('sort', '部门排序')->placeholder('序号越大越靠前')->min(Database::INT_MIN)->max(Database::INT_MAX)->clearable(true);
        $rule[] = FormItem::Textarea('remark', '备注信息')->maxlength(100);
        return Form::create('添加部门')->setRules($rule)->setOptionFormInline(true);
    }


    /**
     * 编辑部门表单
     */
    public function updatedForm($id = 0): Form
    {
        /**
         * @var DepartmentModel $department
         */
        $department = Sys::container(DepartmentService::class)->primaryByFirst($id);
        if ($department) {
            $rule[] = FormItem::Hidden('id', $id);
            $rule[] = FormItem::Input('name', '部门名称', $department->name)->placeholder('后台部门名称')->clearable(true)->maxlength(10)->required('请输入部门名称');
//            $rule[] = FormItem::SearchPanel('manage_id', '部门主管', '/admin-api/filter-panel/depauth-user', $department->manage_id)
//                ->columns([
//                    ['prop' => 'id', 'label' => 'ID'],
//                    ['prop' => 'nickname', 'label' => '名称'],
//                    ['prop' => 'avatar', 'label' => '头像', 'type' => 'image'],
//                ])
//                ->simple(false)
//                ->labelKey('nickname')
//                ->selected(UserModel::class)
//                ->required('请选择部门主管');
            $rule[] = FormItem::Number('sort', '部门排序', $department->sort)->placeholder('序号越大越靠前')->min(Database::INT_MIN)->max(Database::INT_MAX)->clearable(true);
            $rule[] = FormItem::Textarea('remark', '备注信息', $department->remark)->maxlength(100);
            return Form::create('编辑部门')->setRules($rule)->setOptionFormInline(true);
        }
        throw new AppException('部门已失效');
    }
}